/*

Copyright 2009 Nicola Dal Pont

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

package com.astoolkit.workflow
{
	import com.astoolkit.workflow.constant.EMPTY_PIPELINE;
	import com.astoolkit.workflow.constant.EmptyPipelinePolicy;
	import com.astoolkit.workflow.constant.FailurePolicy;
	import com.astoolkit.workflow.constant.Feed;
	import com.astoolkit.workflow.constant.Flow;
	import com.astoolkit.workflow.constant.TaskStatus;
	import com.astoolkit.workflow.constant.UNDEFINED;
	
	import mx.core.IFactory;
	import mx.rpc.Fault;
	
	public class SyncWorkflow extends Workflow implements IWorkflow
	{
		private var _wasAborted : Boolean = false;
		
		public function SyncWorkflow() 
		{
			super();
			_flow = Flow.SERIAL
		}
		
		override public function initialize():void
		{
			super.initialize();
		}
		
		override public function get async() : Boolean
		{
			return false;
		}
		
		override public function prepare():void
		{
			_wasAborted = false;
			if( parent )
				_pipelineData = UNDEFINED;
		}
		
		[Inspectable(defaultValue="serial", enumeration="serial,none")]
		override public function set flow(inFlow:String):void
		{
			if( inFlow != Flow.SERIAL && inFlow != Flow.NONE )
			{
				throw new Error(
					"SyncWorkflow 'flow' property can only be either 'serial' or 'none': " +
					"found '" + inFlow + "'" );
			}
			_flow = inFlow;
		}
		
		override public function begin(inRetain:Boolean=false):void
		{
			_subPipelineData = UNDEFINED;
			if(!_delegate)
				_delegate = createDelegate();
			if( !_parent )
				initialize();
			if( !_parent )
				prepare();
			_status = TaskStatus.RUNNING;
			_delegate.onBegin( this );
			if( !_tasks || _tasks.length == 0 )
			{
				complete();
				return;
			}
			
			var task : IWorkflowTask;
			
			for each( task in _tasks )
				task.prepare();
				
			for each( task in _tasks )
			{
				if( _wasAborted )
				{
					complete();
					return;
				}
				if( task.enabled && ( !(task is IPipelineTask) || ( IPipelineTask( task ).emptyPipelinePolicy != EmptyPipelinePolicy.SKIP || _subPipelineData != EMPTY_PIPELINE ) ) )
				{
					try
					{
						setSubtaskPipelineData( task );
						task.begin( false );
					}
					catch( e : Error )
					{
						if( task.failurePolicy == FailurePolicy.ABORT )
						{
							fail( e.getStackTrace() );
							complete();
							return;
						}
					}
				}
			}
			complete();
			
		}
		
		override protected function complete() : void
		{
			if( _feed == Feed.PIPELINE || _feed == Feed.CURRENT_DATA )
				_pipelineData = _subPipelineData;
			_delegate.onComplete( this );
			if( !parent )
				cleanUp();
		}
		
		override public function abort():void
		{
			_wasAborted = true;
		}
		
		override protected function onSubtaskBegin(inTask:IWorkflowTask):void
		{
			dispatchTaskEvent( WorkflowEvent.STARTED, inTask );
		}
		
		override protected function onSubtaskProgress(inTask:IWorkflowTask):void
		{
			dispatchTaskEvent( WorkflowEvent.STARTED, inTask );
		}
		
		override protected function onSubtaskFault(inTask:IWorkflowTask, inMessage:String, inFault:Fault):void
		{
			dispatchTaskEvent( WorkflowEvent.FAULT, inTask );
			
		}
		
		override protected function onSubtaskAbort(inTask:IWorkflowTask, inMessage:String):void
		{
			dispatchTaskEvent( WorkflowEvent.ABORTED, inTask );
			
		}
		
		override protected function onSubtaskWarning(inTask:IWorkflowTask, inMessage:String, inError:Fault):void
		{
			dispatchTaskEvent( WorkflowEvent.WARNING, inTask );
			
		}
		override protected function onSubtaskCompleted( inTask : IWorkflowTask ) : void
		{
			dispatchTaskEvent( WorkflowEvent.COMPLETED, inTask );
			if( inTask is IPipelineTask )
				_subPipelineData = IPipelineTask( inTask ).taskOutput;
		}
		
	}
}